package com.maven.rabbitmq.topic;

import com.maven.rabbitmq.constant.Constant;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class TopicProduce {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1. 建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("8.135.54.49");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("study");
        connectionFactory.setPassword("study");
        connectionFactory.setVirtualHost("rabbitmq");

        Connection connection = connectionFactory.newConnection();
        //2. 建立信道
        Channel channel = connection.createChannel();

        //3. 声明队列
        channel.queueDeclare(Constant.TOPIC_QUEUE_1, true, false, false, null);
        channel.queueDeclare(Constant.TOPIC_QUEUE_2, true, false, false, null);

        // 声明交换机
        channel.exchangeDeclare(Constant.TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC,true);

        // 4. 绑定队列
        channel.queueBind(Constant.TOPIC_QUEUE_1,Constant.TOPIC_EXCHANGE,"*.a.*");
        channel.queueBind(Constant.TOPIC_QUEUE_2,Constant.TOPIC_EXCHANGE,"*.a.*");
        channel.queueBind(Constant.TOPIC_QUEUE_2,Constant.TOPIC_EXCHANGE,"*.*.b");
        channel.queueBind(Constant.TOPIC_QUEUE_2,Constant.TOPIC_EXCHANGE,"c.#");

        String msg="hello topic……  hello.a.rabbitmq ";
        channel.basicPublish(Constant.TOPIC_EXCHANGE,"hello.a.rabbitmq",null,msg.getBytes());
        msg="hello topic……  hello.a.b ";
        channel.basicPublish(Constant.TOPIC_EXCHANGE,"hello.a.b",null,msg.getBytes());
        msg="hello topic……  hello.rabbitmq.b ";
        channel.basicPublish(Constant.TOPIC_EXCHANGE,"hello.rabbitmq.b",null,msg.getBytes());
        msg="hello topic……  c.b ";
        channel.basicPublish(Constant.TOPIC_EXCHANGE,"c.b",null,msg.getBytes());
        msg="hello topic……  c.hello.rabbitmq ";
        channel.basicPublish(Constant.TOPIC_EXCHANGE,"c.hello.rabbitmq",null,msg.getBytes());

//        channel.close();
//        connection.close();
    }
}
